package com.isfto.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.isfto.dao.ApueDao;
import com.isfto.dao.BeforeFlightChangeEnterAanOutDao;
import com.isfto.dao.FlightChangeOutDao;
import com.isfto.dao.FlightInfoDao;
import com.isfto.entity.tablepojo.*;
import com.isfto.service.FlightChangeOutService;
import com.isfto.service.FlightInfoService;
import com.isfto.until.TimeUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class FlightChangeOutServiceImpl implements FlightChangeOutService {
    @Resource
    FlightChangeOutDao flightChangeOutDao;
    @Resource
    ApueDao apueDao;
    @Resource
    FlightInfoService flightInfoService;
    @Resource
    BeforeFlightChangeEnterAanOutDao beforeFlightChangeEnterAanOutDao;

    @Resource
    FlightInfoDao flightInfoDao;
    @Override
    public int saveInfo(FlightChange flightChange) {
        return flightChangeOutDao.saveInfo(flightChange);
    }

    @Override
    public int updateStauts(FlightChange flightChange) {
        int i = flightChangeOutDao.updateStauts(flightChange);
        return i;
    }

    @Override
    public PageBean flightChangeInfoOUt(String startapcd, String endapcd, String startfptt, String endfplt, Integer page, Integer pageSize) {
        Integer total= flightChangeOutDao.flightChangeCountOUt(startapcd, endapcd, startfptt, endfplt);
        Integer  start=(page-1)*pageSize;
        List<FlightChange> flightChanges = flightChangeOutDao.flightChangeInfoOUt(startapcd, endapcd, startfptt, endfplt, start, pageSize);
        PageBean pageBean=new PageBean(total,flightChanges);
        return pageBean;
    }

    @Override
    public FlightChange afterInfoOut(FlightChange flight) {
        FlightChange flightChange = flightChangeOutDao.afterInfoOut(flight.getCfno(), flight.getStartapcd(),flight.getEndapcd(),flight.getStartfptt(),flight.getEndfplt());
        List<FlightChange> flightChanges = beforeFlightChangeEnterAanOutDao.beforeInfoOut(flight.getCfno(), flight.getStartapcd(), flight.getEndapcd());
        //更新之后的开始时间值
        String upstime= flightChange.getStartfptt();
        //更新之前开始时间值
        String bfstime= flightChanges.get(0).getStartfptt();
        Apue startName = apueDao.startName(flight.getStartapcd());
        flightInfoService.updateInfoStart(upstime,startName.getFlightCode(),bfstime);
        //更新之后到达时间值
        String upetime=flightChange.getEndfplt();
        //更新之前到达时间值
        String bfetime=flightChanges.get(0).getEndfplt();
        //原始开始时间，修改之后开始时间，原始结束时间
        String dateEnd = TimeUtil.getDateEnd(bfstime,upstime,bfetime);
        Apue endName = apueDao.endName(flight.getEndapcd());
        flightInfoService.updateInfoEnd(dateEnd,endName.getFlightCode(),bfetime);
        String flid = flight.getFlid();
        String fide= flightChangeOutDao.fideInfo(flid);
        int i1 = flightChangeOutDao.upFlightChange(dateEnd, flid, flightChange.getCfno());
        List<ABRS> abrs = flightInfoDao.abrsInfos();
        boolean flag=false;
        ABRS abrsnumber=null;
        for (int i = 0; i < abrs.size(); i++) {
            if(abrs.get(i).getCnnm().contains(flightChange.getChangereason())){
                abrsnumber = flightInfoDao.abrsnumber(flightChange.getChangereason());
                flag=true;
                break;
            }
        }
        if(!flag){
            abrsnumber = flightInfoDao.abrsnumber("航班未知原因");
        }
        int upAbst = flightChangeOutDao.upAbst(abrsnumber.getNumber(),"DLYS", fide);
        int upfrtt = flightChangeOutDao.upfrtt(flightChange.getStartfptt(), fide);
        return null;
    }

    @Override
    public FlightChange changeAfterInfoOut(FlightChange flight) {
        FlightChange flightChange = flightChangeOutDao.afterInfoOut(flight.getCfno(), flight.getStartapcd(),flight.getEndapcd(),flight.getStartfptt(), flight.getEndfplt());
        List<FlightChange> flightChanges = beforeFlightChangeEnterAanOutDao.beforeInfoOut(flight.getCfno(), flight.getStartapcd(),flight.getEndapcd());
        //更新之后的开始时间值
        String upstime= flightChange.getStartfptt();
        //更新之前开始时间值
        String bfstime= flightChanges.get(0).getStartfptt();
        Apue startName = apueDao.startName(flight.getStartapcd());
        flightInfoService.updateInfoStart(bfstime,startName.getFlightCode(),upstime);
        //更新之后到达时间值
        String upetime=flightChange.getEndfplt();
        //更新之前到达时间值
        String bfetime=flightChanges.get(0).getEndfplt();
        Apue endName = apueDao.endName(flight.getEndapcd());
        flightInfoService.updateInfoEnd(bfetime,endName.getFlightCode(),upetime);
        String flid = flight.getFlid();
        String fide= flightChangeOutDao.fideInfo(flid);
        flightChangeOutDao.upFlightChange(bfetime, flid, flightChange.getCfno());
        //更新dfdl表中的数据
        int upAbst= flightChangeOutDao.upAbst("", "", fide);
        //更新出发时间
        int upfrtt = flightChangeOutDao.upfrtt(bfstime,fide);
        return null;
    }

    @Override
    public PageBean flightChange(Integer page, Integer pageSize) {
        int total = flightChangeOutDao.total();
        Integer  start=(page-1)*pageSize;
        List<FlightChange> flightChanges = flightChangeOutDao.flightChange(start,pageSize);
        PageBean pageBean=new PageBean(total,flightChanges);
        return pageBean;
    }
    @Override
    public PageBean audittickets(Integer pageno, Integer pageSize, Integer status) {
        PageHelper.startPage( pageno,pageSize );
        Page<Tickets> page=flightChangeOutDao.pageQuery(status);
        PageBean pageBean = new PageBean();
        pageBean.setTotal( (int) page.getTotal() );
        List<Tickets> list = page.getResult();
        pageBean.setRecords( list );
        return pageBean;
    }

    @Override
    public Boolean updatetickertsStauts(Integer id, Integer status) {
        Boolean b=flightChangeOutDao.updateTickets(id,status);
        return b;
    }

    @Override
    public Boolean insert(FlightChange flightChange) {
        flightChange.setName( "manage" );
        flightChange.setEmail( "1351160287@qq.com" );
        flightChange.setStatus( 1);
        Boolean insert = flightChangeOutDao.insert( flightChange );
        return insert;
    }
}
